<?php if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class detail_controller extends PZ_Controller{

	public function __construct() {
		parent::__construct();
		$this->authfunctions = array('');
		$settingapi = load_api('setting');
		$this->colorsetting = $settingapi->getSetting('colorindex');
		$this->sharebuttons = $settingapi->getSetting('sharebutton');
		$this->layoutsetting = $settingapi->getSetting('layoutdetail');
		//$this->share_target = array('sina','qzone','renren','qq','twitter','facebook');
	}

	public function index(){
		$share_id = $this->param('shareid');
		$shareapi = load_api('share');
		$this->share = $shareapi->getShareById($share_id);
		if($this->share){
			$this->share['liked'] = $shareapi->checkonesharefav($this->share);
			$this->share['link'] = host_url(site_url('detail/index?shareid='.$this->share['share_id']));
			$shareapi->addviewnum($share_id);
			$this->nextid = $shareapi->findnextid($share_id);
			$this->preid = $shareapi->findprevid($share_id);
			$this->relation = $this->relationView($this->cuser['uid'], $this->share['user_id']);
			$this->seo_title(sysSubStr($this->share['title'],50,false).' '.$this->share['category_name_cn']);
			$this->seo_keyword($this->share['keyword'].' '.$this->share['category_name_cn']);
			$this->seo_description(sysSubStr($this->share['intro'],200,false));
			$attaches = load_api('attachment')->findAttachByItem($this->share['item_id']);
			$this->share_intro = load_api('puzzcode')->parse_message($this->share['intro'],$attaches,true);
			$imageattaches = array();
			$fileattaches = array();
			$pz_skipaidlist = $GLOBALS['pz_skipaidlist']?$GLOBALS['pz_skipaidlist']:array();
			foreach ($attaches as $att){
				if(in_array($att['aid'], $pz_skipaidlist)){
					continue;
				}
				$att['url'] = thumburl($att['attachment'].'.'.$att['fileext'],'large',$att['cloud']);
				if($att['isimage']){
					$imageattaches[] = $att;
				}else{
					$fileattaches[] = $att;
				}
			}
			$tags = trimarray(explode(' ', $this->share['keyword']));
			$clean = array();
			$maxtag = array_length($tags);
			$maxtag = $maxtag>10?10:$maxtag;
			for ($i=0;$i<$maxtag;$i++){
				$clean[] = $tags[$i];
			}
			unset($tags);
			$this->tags = $clean;
			$this->images = $imageattaches;
			$this->files = $fileattaches;
			if($this->share['reference_url']){
				$this->reflink = httpurl($this->share['reference_url']);
				$urls = parse_url($this->reflink);
				$this->refhost = $urls['host'];
			}

			if($this->layoutsetting['uidetail_samefrom']){
				$sameshares = $shareapi->getsamefromimg($this->share['user_id']);
				$samenum = array_length($sameshares);
				if($samenum<9){
					for ($i=$samenum;$i<9;$i++){
						$sameshares[] = false;
					}
				}
				$this->sameshares = $sameshares;
				$this->samearticles = $shareapi->getsamefromarticle($this->share['user_id']);
			}

			if($this->layoutsetting['uidetail_album']){
				$this->album = load_api('album')->findAlbumByid($this->share['album_id']);
			}

			if($this->layoutsetting['uidetail_recentview']){
				$this->add_view_history($this->share);
				$this->view_history = array_reverse($this->get_view_history());
			}

			if($this->layoutsetting['uidetail_guess']){
				$shares = $shareapi->getRoundByCategory($this->share['category_id'],30);
				$shares = load_api('share')->prepareShares($shares);
				$this->tpl_waterfall = $this->render('waterfall/pin',array('shares'=>$shares));
			}

			if($this->layoutsetting['uidetail_ads']){
				$setting = load_api('setting')->getSetting('ads');
				$this->ads = $setting['detailpage_ad'];
			}
			$this->editperm = ($this->share['user_id']==$this->cuser['uid']||$this->isadmin||$this->iseditor);
			$this->commentperpage = ($this->layoutsetting['uidetail_comment_pagenum'])?$this->layoutsetting['uidetail_comment_pagenum']:5;

			if($this->layoutsetting['uidetail_layout']=="mini"){
				$this->output("detail/index_mini");
			}else{
				$this->output("detail/index");
			}
		}else{
			$this->message(lang('404_error_heading','tip'), lang('404_error_content','tip'));
			return ;
		}
	}

	private function get_view_history(){
		//$this->input->set_cookie('view_history', array_to_str($remember), 86400*365);
		$arr = array();
		$view_history = $this->input->cookie(config_item('cookie_prefix').'view_history');
		if($view_history){
			return str_to_arr_list($view_history);
		}else{
			return array();
		}
	}

	private function add_view_history($share){
		$history = $this->get_view_history();
		$value['s'] =  $share['share_id'];
		$value['p'] = $share['image_path_0'];
		$value['t'] = sysSubStr($share['title'],20,true);
		$value['m'] = $share['total_comment'];
		$value['k'] = $share['total_like'];
		$value['c'] = $share['cloud'];

		foreach ($history as $i=>$his) {
			if($his['s']==$value['s']){
				$index = $i;
				break;
			}
		}
		if(is_numeric($index)){
			array_splice($history, $index,1);
		}

		if(count($history)>5){
			array_shift($history);
		}
		array_push($history, $value);

		$this->input->set_cookie('view_history', arr_list_to_str($history), 604800);
	}

}